#
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

package(default_visibility = ["//zetasql/base:zetasql_implementation"])

filegroup(
    name = "type_headers",
    srcs = [
        "annotation.h",
        "array_type.h",
        "collation.h",
        "container_type.h",
        "extended_type.h",
        "list_backed_type.h",
        "map_type.h",
        "measure_type.h",
        "range_type.h",
        "row_type.h",
        "simple_type.h",
        "simple_value.h",
        "struct_type.h",
        "type.h",
        "type_modifiers.h",
        "type_parameters.h",
        "value_equality_check_options.h",
    ],
)

cc_library(
    name = "type",
    srcs = [
        "annotation.cc",
        "array_type.cc",
        "collation.cc",
        "extended_type.cc",
        "internal_utils.h",
        "map_type.cc",
        "measure_type.cc",
        "range_type.cc",
        "row_type.cc",
        "simple_type.cc",
        "simple_value.cc",
        "struct_type.cc",
        "type.cc",
        "type_modifiers.cc",
        "type_parameters.cc",
    ],
    hdrs = [":type_headers"],
    deps = [
        ":timestamp_util",
        ":value_representations",
        "//zetasql/base",
        "//zetasql/base:case",
        "//zetasql/base:check",
        "//zetasql/base:compact_reference_counted",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base:time_proto_util",
        "//zetasql/common:errors",
        "//zetasql/common:float_margin",
        "//zetasql/common:string_util",
        "//zetasql/common:thread_stack",
        "//zetasql/common:unicode_utils",
        "//zetasql/common/search/public:token_list_util",
        "//zetasql/public:annotation_cc_proto",
        "//zetasql/public:civil_time",
        "//zetasql/public:collation_cc_proto",
        "//zetasql/public:interval_value",
        "//zetasql/public:json_value",
        "//zetasql/public:language_options",
        "//zetasql/public:numeric_value",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:pico_time",
        "//zetasql/public:simple_value_cc_proto",
        "//zetasql/public:strings",
        "//zetasql/public:timestamp_picos_value",
        "//zetasql/public:type_annotation_cc_proto",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:type_modifiers_cc_proto",
        "//zetasql/public:type_parameters_cc_proto",
        "//zetasql/public:uuid_value",
        "//zetasql/public:value_cc_proto",
        "//zetasql/public:value_content",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/functions:rounding_mode_cc_proto",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:no_destructor",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:node_hash_map",
        "@com_google_absl//absl/container:node_hash_set",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/meta:type_traits",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:optional",
        "@com_google_absl//absl/types:span",
        "@com_google_absl//absl/types:variant",
        "@com_google_protobuf//:protobuf",
    ],
)

# This includes parts of the ":type" library that include Catalog dependencies.
# These are in a separate build target to avoid circular dependencies.
# These methods are only needed in analyzer code that actually calls them.
cc_library(
    name = "type_with_catalog_impl",
    srcs = ["row_type_with_catalog.cc"],
    deps = [
        ":type",
        "//zetasql/public:catalog",
    ],
    alwayslink = 1,
)

cc_library(
    name = "internal_proto_utils",
    srcs = ["internal_proto_utils.cc"],
    hdrs = ["internal_proto_utils.h"],
    deps = [
        ":type",
        "//zetasql/base:logging",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:proto_helper",
        "//zetasql/public:options_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_protobuf//:protobuf",
    ],
)

filegroup(
    name = "type_factory_headers",
    srcs = [
        "enum_type.h",
        "graph_element_type.h",
        "graph_path_type.h",
        "internal_proto_utils.h",
        "proto_type.h",
        "type_deserializer.h",
        "type_factory.h",
    ],
)

cc_library(
    name = "type_factory",
    srcs = [
        "enum_type.cc",
        "graph_element_type.cc",
        "graph_path_type.cc",
        "internal_utils.h",
        "proto_type.cc",
        "type_deserializer.cc",
        "type_factory.cc",
    ],
    hdrs = [
        ":type_factory_headers",
    ],
    deps = [
        ":internal_proto_utils",
        ":type",
        ":value_representations",
        "//zetasql/base",
        "//zetasql/base:case",
        "//zetasql/base:check",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:errors",
        "//zetasql/common:float_margin",
        "//zetasql/common:proto_helper",
        "//zetasql/common:thread_stack",
        "//zetasql/public:annotation_cc_proto",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:strings",
        "//zetasql/public:type_annotation_cc_proto",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value_cc_proto",
        "//zetasql/public:value_content",
        "//zetasql/public/functions:array_find_mode_cc_proto",
        "//zetasql/public/functions:array_zip_mode_cc_proto",
        "//zetasql/public/functions:bitwise_agg_mode_cc_proto",
        "//zetasql/public/functions:convert_proto",
        "//zetasql/public/functions:datetime_cc_proto",
        "//zetasql/public/functions:differential_privacy_cc_proto",
        "//zetasql/public/functions:normalize_mode_cc_proto",
        "//zetasql/public/functions:range_sessionize_mode_cc_proto",
        "//zetasql/public/functions:rank_type_cc_proto",
        "//zetasql/public/functions:rounding_mode_cc_proto",
        "//zetasql/public/functions:unsupported_fields_cc_proto",
        "//zetasql/public/proto:type_annotation_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:inlined_vector",
        "@com_google_absl//absl/container:node_hash_map",
        "@com_google_absl//absl/container:node_hash_set",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/functional:function_ref",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/meta:type_traits",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "types",
    hdrs = [
        ":type_factory_headers",
        ":type_headers",
    ],
    deps = [
        ":timestamp_util",
        ":type",
        ":type_factory",
        ":value_representations",
        "//zetasql/base",
        "//zetasql/base:case",
        "//zetasql/base:check",
        "//zetasql/base:map_util",
        "//zetasql/base:status",
        "//zetasql/common:errors",
        "//zetasql/common:float_margin",
        "//zetasql/common:thread_stack",
        "//zetasql/public:annotation_cc_proto",
        "//zetasql/public:civil_time",
        "//zetasql/public:collation_cc_proto",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:simple_value_cc_proto",
        "//zetasql/public:type_annotation_cc_proto",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:type_modifiers_cc_proto",
        "//zetasql/public:type_parameters_cc_proto",
        "//zetasql/public/proto:type_annotation_cc_proto",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:inlined_vector",
        "@com_google_absl//absl/container:node_hash_map",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:optional",
        "@com_google_absl//absl/types:span",
        "@com_google_absl//absl/types:variant",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "value_representations",
    hdrs = [
        "value_representations.h",
    ],
    deps = [
        "//zetasql/base:compact_reference_counted",
        "//zetasql/base:logging",
        "//zetasql/base:strings",
        "//zetasql/public:interval_value",
        "//zetasql/public:json_value",
        "//zetasql/public:numeric_value",
        "//zetasql/public:simple_token_list",
        "//zetasql/public:timestamp_picos_value",
        "//zetasql/public:uuid_value",
        "//zetasql/public:value_content",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/strings:cord",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/types:optional",
        "@com_google_absl//absl/types:span",
        "@com_google_absl//absl/types:variant",
    ],
)

cc_library(
    name = "timestamp_util",
    srcs = [
        "timestamp_util.cc",
    ],
    hdrs = [
        "timestamp_util.h",
    ],
    deps = [
        "@com_google_absl//absl/time",
    ],
)

cc_test(
    name = "graph_element_type_test",
    srcs = ["graph_element_type_test.cc"],
    deps = [
        ":types",
        "//zetasql/base:check",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:type_parameters_cc_proto",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/status",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "graph_path_type_test",
    srcs = ["graph_path_type_test.cc"],
    deps = [
        ":types",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common/testing:proto_matchers",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:type_parameters_cc_proto",
        "@com_google_absl//absl/hash:hash_testing",
        "@com_google_absl//absl/status",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "map_type_test",
    srcs = ["map_type_test.cc"],
    deps = [
        ":types",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:type_parameters_cc_proto",
        "//zetasql/public:value",
        "//zetasql/testdata:test_schema_cc_proto",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/status",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "measure_type_test",
    srcs = ["measure_type_test.cc"],
    deps = [
        ":types",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:options_cc_proto",
        "//zetasql/testdata:test_schema_cc_proto",
        "@com_google_absl//absl/status",
    ],
)
